`
https://leetcode.cn/problems/spiral-matrix-ii/
`

/**
 * @param {number} n
 * @return {number[][]}
 */
var generateMatrix = function (n) {
  const res = new Array(n).fill(null).map(() => new Array(n))

  let left = 0, right = n - 1, top = 0, bottom = n - 1

  // 计数器
  let count = 1

  while (count <= n * n) {
    for (let i = left; i <= right; i++) {
      res[top][i] = count++
    }
    top++
    for (let i = top; i <= bottom; i++) {
      res[i][right] = count++
    }
    right--
    for (let i = right; i >= left; i--) {
      res[bottom][i] = count++
    }
    bottom--
    for (let i = bottom; i >= top; i--) {
      res[i][left] = count++
    }
    left++
  }

  return res
};